﻿///  Copyright (c) 2009 Ray Liang (http://www.dotnetage.com)
///  Dual licensed under the MIT and GPL licenses:
///  http://www.opensource.org/licenses/mit-license.php
///  http://www.gnu.org/licenses/gpl.html

//Notes:This script is not use in beta1 

(function ($) {
    $.widget("ui.widgetExplorer", {
        ///<smmary>The explorer that contains all the category widgets to add to the page.</summary>
        _create: function () {
            var self = this, opts = this.options, el = this.element,
            _dlg = $(".ui-widget-preview", el),
            _list = $(".ui-listview", el),
            _zones = portal.dynamicUI.zones,
            _defaultZone = null;

            var jDialog = _dlg.dialog({
                autoOpen: false,
                show: "slide",
                hide: "slide",
                width: 685,
                position: ["center", "top"],
                buttons: {
                    "Add to page": function () {
                        if (portal.dynamicUI.zones) {
                            if (portal.dynamicUI.zones.length > 0) {
                                _defaultZone = $(portal.dynamicUI.zones[0]);
                            }
                        }
                        portal.dynamicUI.addTo({
                            wid: _list.listview("option", "selectedItem").value.split(",")[0],
                            url: document.URL,
                            zoneID: _defaultZone.attr("id"),
                            pos: 0
                        }, function (txt) {
                            if (txt) {
                                var wo = JSON.parse(txt);
                                if (wo) {
                                    $nw = $("<div></div>");
                                    _defaultZone.prepend($nw);
                                    $nw.portlet(wo);
                                }
                            }
                            _dlg.dialog("close");
                        });
                    }
                }
            });

            if (_zones.length) {
                var _zoneDropdown = $("<ul/>").appendTo($(".ui-dialog-buttonpane", _dlg.dialog('widget')));
                _zones.each(function (i, n) {
                    var _zItem = $("<li/>").appendTo(_zoneDropdown),
                    _zVal = $("<input type='hidden' />").appendTo(_zItem).val(n.attr("id")),
                    _zTxt = $("<span/>").appendTo(_zItem);
                    _zTxt.text(n.attr("title") ? n.attr("title") : n.attr("id"));
                });
                _zoneDropdown.dropdown({ selectedValue: _defaultZone.attr("id") });
            }

            _list.listview({
                items: "ul>li",
                itemClick: function (event, ui) {
                    var _args = ui.value.split(",");
                    $(".ui-content-detail").load("/Widget/Info", { id: _args[0] });
                    var $preview = $(".ui-content-preview");
                    var wp = $("<div></div>");
                    $preview.html("");
                    $preview.append(wp);
                    wp.portlet({
                        id: _args[0],
                        url: _args[1],
                        closable: false,
                        deletable: false,
                        expanded: true,
                        showHeader: true,
                        showBorder: true,
                        preview: true,
                        title: ui.text
                    });
                    _dlg.dialog("open");
                }
            });
        }
    });
})(jQuery);